<template>
	<view class="container" style="display: flex;flex-direction: column;gap: 20rpx;padding-bottom: 120rpx;">
		<u--form ref="queryForm" :model="query" label-width="120rpx">
		  <u-form-item label="物料码" prop="labelNo">
		    <u--input 
		    	placeholder="请输入/扫描" 
		    	v-model.trim="query.labelNo" 
		    	suffixIcon="scan" 
		    	suffixIconStyle="font-size: 68rpx"
		    	style="flex: 1;"
					@confirm="extraInfo"
		    >
		    </u--input>
		  </u-form-item>
		</u--form>
		<uni-card v-for="(item, index) in list" :key="index">
			<view class="inner-container">
				<view>目标储位：{{ item.destinationCode }}</view>
				<view>物料号：{{ item.materialCode }}</view>
				<view>待绑定数量：<text style="color: #e45656;">{{ item.bindNum }}</text></view>
				<view>已绑定数量：<text style="color: #53c21d;">{{ item.alreadyTotal }}</text></view>
				<view>差值：{{ item.waitBindNum }}</view>
			</view>
		</uni-card>
		<view class="commonButton">
			<u-button type="error" @click="forceCall">强制叫车</u-button>
			<u-button type="primary" :disabled="Object.keys(scanMaterial).length === 0" @click="bindCommonMaterial">确认绑定</u-button>
		</view>
		<u-toast ref="uToast"></u-toast>
		<!-- modal模态框 -->
		<u-modal
			:show="show"
			:showCancelButton="true"
			:closeOnClickOverlay="false"
			:content="content"
			@confirm="confirmButton"
			@cancel="cancelButton"
			ref="uModal"
			:asyncClose="true"
		></u-modal>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	import { checkRackAPI, callCarAPI, getNeedSortListAPI } from '@/api/work/outbound'
	import {
		uniCard
	} from '@dcloudio/uni-ui'
	export default {
		components: {
			uniCard
		},
		data() {
			return {
				query: {
					labelNo: ''
				},
				list: [],
				rackCode: '',
				scanMaterial: '',
				show: false,
				content: ''
			};
		},
		onLoad(options) {
			this.rackCode = options.rackCode
		},
		computed: {
			...mapState(['macAddress'])
		},
		mounted() {
			this.toQuery()
		},
		methods: {
			async toQuery() {
				const submitForm = {
					url: this.macAddress,
					rackCode: this.rackCode
				}
				const res = await getNeedSortListAPI(submitForm)
				if (res.code === 200) {
					this.list = res.data.details
					this.rackCode = res.data.rackCode
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			async bindCommonMaterial() {
				const submitForm = {
					macUrl: this.macAddress,
					rackCode: this.rackCode,
					materialReelId: this.query.labelNo
				}
				const res = await getNeedSortListAPI(submitForm)
				if (res.code === 200) {
					// console.log(res)
					this.query = {
						labelNo: ''
					}
					this.scanMaterial = {}
					this.list = res.data.details
					this.rackCode = res.data.rackCode
					this.$refs.uToast.show({
						type: 'success',
						message: '操作成功'
					})
					// this.toQuery()
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			extraInfo() {
				if (!this.query.labelNo.includes('*')) {
					this.query.labelNo = ''
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				const array = this.query.labelNo.split('*')
				if (array.length !== 6) {
					this.query.labelNo = ''
					return this.$refs.uToast.show({
						type: 'warning',
						message: '请扫描正确的物料二维码'
					})
				}
				// 物料码 批次 日期 供应商 数量 reelId
				this.scanMaterial = {
					materialCode: array[0],
					materialBatch: array[1],
					date: array[2],
					supplier: array[3],
					materialQty: Number(array[4]),
					reelId: array[5]
				}
				this.query.labelNo = this.scanMateial.reelId
			},
			cancelButton() {
				this.show = false
				this.content = ''
			},
			confirmButton() {
				this.callCar()
			},
			async forceCall() {
				const submitForm = {
					rackCode: this.rackCode,
					url: this.macAddress
				}
				// 判断料是否检测完毕
				const res = await checkRackAPI(submitForm)
				// console.log(res)
				if (res.code === 200) {
					if (res.data === false) {
						// 弹窗
						this.show = true
						this.content = '该料架尚有物料未出完, 是否继续?'
					} else {
						// 直接叫车
						this.callCar()
					}
				} else {
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			},
			async callCar() {
				const submitForm = {
					rackCode: this.rackCode,
					url: this.macAddress
				}
				const res = await callCarAPI(submitForm)
				if (res.code === 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: '操作成功'
					})
					this.show = false
				} else {
					this.show = false
					return this.$refs.uToast.show({
						type: 'warning',
						message: res.message
					})
				}
			}
		}
	}
</script>

<style lang="scss">
::v-deep uni-view.uni-card {
	margin: 0 !important;
}
.inner-container {
	display: flex;
	flex-direction: column;
	font-size: 36rpx;
	// gap: 20rpx;
}
.commonButton {
	width: 100%;
	display: flex;
	gap: 20rpx;
	position: fixed;
	left: 0;
	bottom: 0;
	box-sizing: border-box;
	padding: 15rpx;
	justify-content: space-between;
}
</style>
